package com.ruoyix.im.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.im.domain.bo.ImChatUserGroupMessageBo;
import com.ruoyix.im.domain.vo.ImChatUserGroupMessageVo;
import com.ruoyix.im.service.IImChatUserGroupMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 群聊记录
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/chatUserGroupMessage")
public class ImChatUserGroupMessageController extends BaseController {

    private final IImChatUserGroupMessageService iImChatUserGroupMessageService;

    /**
     * 查询群聊记录列表
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:list")
    @GetMapping("/list")
    public TableDataInfo<ImChatUserGroupMessageVo> list(ImChatUserGroupMessageBo bo, PageQuery pageQuery) {
        return iImChatUserGroupMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出群聊记录列表
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:export")
    @Log(title = "群聊记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImChatUserGroupMessageBo bo, HttpServletResponse response) {
        List<ImChatUserGroupMessageVo> list = iImChatUserGroupMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "群聊记录", ImChatUserGroupMessageVo.class, response);
    }

    /**
     * 获取群聊记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:query")
    @GetMapping("/{id}")
    public R<ImChatUserGroupMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iImChatUserGroupMessageService.queryById(id));
    }

    /**
     * 新增群聊记录
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:add")
    @Log(title = "群聊记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImChatUserGroupMessageBo bo) {
        return toAjax(iImChatUserGroupMessageService.insertByBo(bo));
    }

    /**
     * 修改群聊记录
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:edit")
    @Log(title = "群聊记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImChatUserGroupMessageBo bo) {
        return toAjax(iImChatUserGroupMessageService.updateByBo(bo));
    }

    /**
     * 删除群聊记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:chatUserGroupMessage:remove")
    @Log(title = "群聊记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iImChatUserGroupMessageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取群消息（只获取前四十条）
     */
    @GetMapping("/listGroupMessage")
    public R<Page> listGroupMessage(@RequestParam(value = "current", defaultValue = "1") Long current,
                                               @RequestParam(value = "size", defaultValue = "40") Long size,
                                               @RequestParam(value = "groupId") Long groupId) {
        return iImChatUserGroupMessageService.getPagePoetryResult(current, size, groupId);
    }


}
